package com.clduo.test.myexpandabledemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.clduo.test.myexpandabledemo.QQListView.QQHeaderAdapter;
import com.clduo.test.myexpandabledemo.refresh.RefreshableView;
import com.clduo.test.myexpandabledemo.refresh.RefreshableView.RefreshListener;

public class MainActivity extends Activity {

	private RefreshableView mRefreshableView;
	private QQListView exList;
	private myAdapter adapter;
	private List<Data> datas;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		datas = new ArrayList<Data>();
		for (int i = 1; i < 5; i++) {
			Data data = new Data();
			List<Child> childs = new ArrayList<Child>();
			for (int j = 1; j < 10; j++) {
				Child child = new Child();
				child.setName("目录" + j);
				childs.add(child);
			}
			data.setGroup("分组" + i);
			data.setChilds(childs);
			datas.add(data);
		}

		exList = (QQListView) findViewById(R.id.home_expandableListView);
		exList.setHeaderView(getLayoutInflater().inflate(R.layout.group, exList, false));
		adapter = new myAdapter(this, exList);
		exList.setAdapter(adapter);

		//展开所有的View
		for (int i = 0, count = adapter.getGroupCount(); i < count; i++) {
			exList.expandGroup(i);
		}

		//下拉刷新
		mRefreshableView = (RefreshableView) findViewById(R.id.refresh_root);
		mRefreshableView.setRefreshListener(new RefreshListener() {
			public void onRefresh(RefreshableView view) {
				new Handler().postDelayed(new Runnable() {
					public void run() {
						mRefreshableView.finishRefresh();
						for (int i = 0, count = adapter.getGroupCount(); i < count; i++) {
							exList.expandGroup(i);
						}
					}
				}, 500);
			}
		});
	}

	private class myAdapter extends BaseExpandableListAdapter implements QQHeaderAdapter {
		private QQListView listView;
		private Context context;

		public myAdapter(Context context, QQListView listView) {
			this.context = context;
			this.listView = listView;
		}

		@Override
		public int getGroupCount() {
			return datas.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return datas.get(groupPosition).getChilds().size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return datas.get(groupPosition);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return datas.get(groupPosition).getChilds().get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.group, null);
			}
			ImageView iv = (ImageView) convertView.findViewById(R.id.groupIcon);
			TextView text = (TextView) convertView.findViewById(R.id.groupto);
			text.setText(datas.get(groupPosition).getGroup());
			if (isExpanded) {
				iv.setImageResource(R.drawable.btn_browser2);
			} else {
				iv.setImageResource(R.drawable.btn_browser);
			}
			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.child, null);
			}
			TextView text = (TextView) convertView.findViewById(R.id.childto);
			text.setText(datas.get(groupPosition).getChilds().get(childPosition).getName());
			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		@Override
		public int getQQHeaderState(int groupPosition, int childPosition) {
			final int childCount = getChildrenCount(groupPosition);
			if (childPosition == childCount - 1) {
				return PINNED_HEADER_PUSHED_UP;
			} else if (childPosition == -1 && !listView.isGroupExpanded(groupPosition)) {
				return PINNED_HEADER_GONE;
			} else {
				return PINNED_HEADER_VISIBLE;
			}
		}

		@Override
		public void configureQQHeader(View header, int groupPosition, int childPosition, int alpha) {
			Data groupData = (Data) this.getGroup(groupPosition);
			((TextView) header.findViewById(R.id.groupto)).setText(groupData.getGroup());
		}

		private HashMap<Integer, Integer> groupStatusMap = new HashMap<Integer, Integer>();

		@Override
		public void setGroupClickStatus(int groupPosition, int status) {
			groupStatusMap.put(groupPosition, status);
		}

		@Override
		public int getGroupClickStatus(int groupPosition) {
			if (groupStatusMap.containsKey(groupPosition)) {
				return groupStatusMap.get(groupPosition);
			} else {
				return 0;
			}
		}
	}
}
